using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using JetBrains.Annotations;
using Kaleida.ServiceMonitor.Framework;

namespace Kaleida.ServiceMonitor.Model.Runtime
{
    public class CompiledScript
    {
        public static readonly Duration DefaultPollFrequency = new Duration(1000, "ms");

        private Duration pollFrequency = DefaultPollFrequency;

        private readonly List<IOperation> operations = new List<IOperation>();
        private OperationOrder executionOrder = OperationOrder.RoundRobin;
        private IList<MailAddress> emailOnThresholdChangeRecipients = new List<MailAddress>();
    	private EmailSummarySchedule emailSummarySchedule = new EmailSummarySchedule(Enumerable.Empty<MailAddress>(), Enumerable.Empty<ScheduledTime>());

    	public List<IOperation> Operations
        {
            get { return operations; }
        }
        
        public OperationOrder ExecutionOrder
        {
            get { return executionOrder; }
            set { executionOrder = value; }
        }

        [NotNull]
        public Duration PollFrequency
        {
            get { return pollFrequency; }
            set
            {
                if (value == null) throw new ArgumentNullException("value");

                pollFrequency = PollFrequencyLimit.EnsureMet(value);
            }
        }

        [NotNull]
        internal IList<MailAddress> EmailOnThresholdChangeRecipients
        {
            get { return emailOnThresholdChangeRecipients; }
            set
            {
                if (value == null) throw new ArgumentNullException("value");
                emailOnThresholdChangeRecipients = value;
            }
        }

    	[NotNull]
		public EmailSummarySchedule EmailSummarySchedule
    	{
			get { return emailSummarySchedule; }
    		set
    		{
    			if (value == null) throw new ArgumentNullException("value");
    			emailSummarySchedule = value;
    		}
    	}

    	public string BuildSummary()
        {
            var summary = new StringBuilder();
            summary.AppendFormat("{0} operation{1} will be performed ", Operations.Count(), Operations.Count() == 1 ? "" : "s");
            summary.AppendFormat("in {0} order, initially with {1} between each invocation. Frequency can be altered whilst running.\r\n", ExecutionOrder.Name, PollFrequency);

            if (emailOnThresholdChangeRecipients.Any())
            {
                summary.Append("When the monitoring threshold changes (e.g from green to yellow), emails will be sent to: ");
                summary.Append(string.Join("; ", emailOnThresholdChangeRecipients));
                summary.AppendLine();
            }

			if(emailSummarySchedule.Recipients.Any())
			{
                var timesOfDaySummary = string.Join(", ", emailSummarySchedule.Times);
				summary.AppendFormat("At {0} every day, a summary email will be sent to: ", timesOfDaySummary);
				summary.Append(string.Join("; ", emailSummarySchedule.Recipients));
                summary.AppendLine();
			}

            return summary.ToString();
        }
    }
}